package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.dao.PositionInfoDao;
import io.renren.dto.PositionInfoDto;
import io.renren.entity.PositionInfoEntity;
import io.renren.entity.RichtextContentEntity;
import io.renren.service.PositionInfoservice;
import io.renren.util.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("positionInfoservice")
public class PositionInfoserviceImpl extends ServiceImpl<PositionInfoDao, PositionInfoEntity> implements PositionInfoservice {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Map<String, Object> newParams = new HashMap<>();
        if (params.get("limit") != null) {
            newParams.put("limit", params.get("limit"));
        }
        if (params.get("page") != null) {
            newParams.put("page", params.get("page"));
        }


        QueryWrapper wrapper = new QueryWrapper<PositionInfoEntity>();
        wrapper.eq("enabled", 0);
        wrapper.eq("position_type", String.valueOf(params.get("positionType")));
        wrapper.orderByDesc("update_time");


        if (params.get("name") != null && !StringUtils.isEmpty(params.get("name").toString())) {
            wrapper.like("position_name", params.get("name").toString());
        }
        IPage<PositionInfoEntity> page = this.page(
                new Query<PositionInfoEntity>().getPage(newParams),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 公司官网查询内容列表
     *
     * @param params
     * @return 返回列表数据
     */
    public Map<String, Object> querList(Map<String, Object> params) {

        PageUtils pageUtils = queryPage(params);
        Map<String, Object> map = new HashMap<>();
        if (pageUtils != null) {
            map.put("totalCount", pageUtils.getTotalCount());
            map.put("currPage", pageUtils.getCurrPage());
            map.put("totalPage", pageUtils.getTotalPage());
            if (pageUtils.getList() != null && !pageUtils.getList().isEmpty()) {
                List<PositionInfoDto> positionInfoDtos = new ArrayList<>();
                for (int index = 0; index < pageUtils.getList().size(); index++) {
                    PositionInfoEntity entity = (PositionInfoEntity) pageUtils.getList().get(index);
                    PositionInfoDto dto = new PositionInfoDto();
                    dto.setId(entity.getId());
                    dto.setPositionName(entity.getPositionName());
                    dto.setPositionType(entity.getPositionType());
                    dto.setPositionContent(entity.getPositionContent());
                    positionInfoDtos.add(dto);
                }
                map.put("list", positionInfoDtos);
            }
        }
        return map;
    }

}
